//
//  DetaiViewController.m
//  Travel assistant
//
//  Created by 杨帆 on 16/3/14.
//  Copyright © 2016年 旅游助手. All rights reserved.
//

#import "DetaiViewController.h"
#import "DetailHeardView.h"
#import "DetailBannerModel.h"
#import "DetailSectionModel.h"
#import "DetailDaysModel.h"
#import "DetailCell.h"
#import <UMSocial.h>
#import "PicViewerVC.h"

@interface DetaiViewController ()<UITableViewDataSource,UITableViewDelegate,UMSocialUIDelegate>

@property(nonatomic,strong)UITableView *tableView;
// banner array
@property (nonatomic, strong) NSMutableArray *bannerArray;

// cell array
@property (nonatomic, strong) NSMutableArray *listArray;

// 图片数据
@property (nonatomic, strong) NSMutableArray *imgArray;

// section array
@property (nonatomic, strong) NSMutableArray *sectionArray;

@property (nonatomic,strong) DetailHeardView *heardView;

@end

@implementation DetaiViewController


-(void)viewWillAppear:(BOOL)animated{
    
    
    self.navigationController.navigationBar.hidden = NO;
    
    
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];

    
    [self addTableViews];

    [self makeDataSource];


    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(shareAction:)];
    
    
    UISwipeGestureRecognizer *swip = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(backAction:)];
    swip.direction = UISwipeGestureRecognizerDirectionRight;
    
    [self.tableView addGestureRecognizer:swip];
    
}


-(void)backAction:(UISwipeGestureRecognizer *)sender{
    
    
    [self popoverPresentationController];
    
}


-(void)addTableViews{
    
    self.bannerArray = [NSMutableArray array];
    self.listArray = [NSMutableArray array];
    self.sectionArray = [NSMutableArray array];
    self.imgArray = [NSMutableArray array];
    
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    
    self.heardView = [[DetailHeardView alloc]initWithFrame:(CGRectMake(0, 0, self.view.width, self.view.height*0.599))];
    self.tableView.tableHeaderView = self.heardView;
    
    
    [self.tableView registerClass:[DetailCell class] forCellReuseIdentifier:@"cell"];
    
    
}

-(void)shareAction:(UIBarButtonItem *)sender{
    
    
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"52b13dc556240b55870e7148"
                                      shareText:@"友盟社会化分享让您快速实现分享等社会化功能，http://umeng.com/social"
                                     shareImage:nil
                                shareToSnsNames:@[UMShareToSina,UMShareToQQ,UMShareToQzone,UMShareToWechatSession,UMShareToWechatTimeline]
                                       delegate:self];
    
    
    
    
    
}



-(void)makeDataSource{
    
    NSString *url =[NSString stringWithFormat:@"http://api.breadtrip.com/trips/%@/waypoints/?gallery_mode=1&sign=e27ff5dd6cefbda2efc343868822d433",self.urlString];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    [manager GET:url parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        [self parserData:responseObject];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];

    
}


- (void)parserData:(NSDictionary *)dict{
    
    self.imgArray = [NSMutableArray array];
    
    
    DetailBannerModel *model = [DetailBannerModel new];
    
    model.trackpoints_thumbnail_image = [dict objectForKey:@"trackpoints_thumbnail_image"];
    model.day_count = [dict objectForKey:@"day_count"];
    model.first_day = [dict objectForKey:@"first_day"];
    model.mileage = [dict objectForKey:@"mileage"];
    model.name = [dict objectForKey:@"name"];
    model.recommendations = [dict objectForKey:@"recommendations"];
    //tableHeaderView需要用的数据源
    [self.bannerArray addObject:model];
    //把需要的数据源传递过去
    [self.heardView setData:model];
    
    
    NSArray *days = [dict objectForKey:@"days"];
    for (NSDictionary *dic in days) {
        DetailSectionModel *m = [[DetailSectionModel alloc]init];
        m.date = [dic objectForKey:@"date"];
        m.day = [dic objectForKey:@"day"];
        m.waypoints = [dic objectForKey:@"waypoints"];
        //区头需要用到的数据源
        [self.sectionArray addObject:m];
        NSArray *waypoints = [dic objectForKey:@"waypoints"];
        for (NSDictionary *dic in waypoints) {
            NSString *photo = [dic objectForKey:@"photo"];
            
            DetailDaysModel *model = [[DetailDaysModel alloc]init];
            model.photo = photo;
            model.text = [dic objectForKey:@"text"];
            model.local_time = [dic objectForKey:@"local_time"];
            [self.listArray addObject:model];
            
            // 添加图片
            if (NO == isEmptyString(photo)) {
                [self.imgArray addObject:[dic objectForKey:@"photo"]];
            }
            
        }
        
    }
    
    dispatch_async(dispatch_get_main_queue(), ^{
        
        [self.tableView reloadData];
    });
    
    
}





- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.sectionArray.count;
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    DetailSectionModel *model = self.sectionArray[section];
    
    return model.waypoints.count;
    
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    DetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (cell == nil) {
        
        cell = [[DetailCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"cell"];
    }
    
    DetailDaysModel *m = self.listArray[indexPath.row];
    
    [cell setData:m];
    return cell;
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    DetailCell *cell = [[ DetailCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
    DetailDaysModel *item = self.listArray[indexPath.row];
    return [cell setData:item];
    
    
    
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    DetailSectionModel *model = self.sectionArray[section];
    NSString *string = [NSString stringWithFormat:@"第%@天 %@",[model.day stringValue],model.date];
    return string;
}



- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 22;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    PicViewerVC *picVC = [PicViewerVC new];
    
    picVC.arrayLargeImages = self.imgArray;
    
    picVC.curImageIndex = indexPath.row;
    
    [self.navigationController presentViewController:picVC animated:YES completion:nil];
    
    
}

@end
